package edu.ts.crowdfunding.product.Controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.ts.crowdfunding.product.domain.PO.ConditionalQueryPO;
import edu.ts.crowdfunding.product.domain.PO.ProductPO;
import edu.ts.crowdfunding.product.domain.Product;
import edu.ts.crowdfunding.product.service.ProductService;
import edu.ts.crowdfunding.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
@Tag(name = "商品相关接口", description = "商品接口")
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     *
     * 项目名称/发起人
     *  项目状态
     *   项目分类
     */
    @PostMapping("/list/{pageNum}/{pageSize}")
    @Operation(summary = "分页查询",description = "分页查询")
    public Result list(@PathVariable Integer pageNum, @PathVariable Integer pageSize, @RequestBody ConditionalQueryPO conditionalQueryPO){
        return productService.pageSearch(pageNum,pageSize,conditionalQueryPO);
    }

    @PostMapping("/save")
    @Operation(summary = "保存商品",description = "保存商品")
    public Result save(@RequestBody ProductPO productPO){
        return productService.add(productPO);
    }


    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除商品",description = "删除商品接口")
    public Result delete(@PathVariable Long id){
        Result result = new Result<>();
        try {
            productService.removeById(id);
            result.setCode(1);
        } catch (Exception e) {
            result.setCode(0);
            throw new RuntimeException(e);
        }
        return result;
    }


    @GetMapping("/get/{id}")
    @Operation(summary = "获取产品信息", description = "成功:1 失败:0")
    public Result get(@PathVariable Long id) {
        Result result = new Result();
        try {
            Product product= productService.getProductRef(id);

            if(product != null ){
                result.setData(product);
                result.setCode(1);
            }else {
                result.setCode(0);
            }

        } catch (Exception e) {
            result.setCode(0);
            throw new RuntimeException(e);
        }
        return result;
    }

    @PatchMapping("/update")
    @Operation(summary = "更新商品信息", description = "成功:1 失败:0")
    public Result update(@RequestBody Product product) {
        Result result = new Result();
        try {
            productService.updateById(product);
            result.setCode(1);
        } catch (Exception e) {
            result.setCode(0);
            throw new RuntimeException(e);
        }
        return result;
    }


}
